<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="入驻商户名称" prop="userName" >
        <el-input
          v-model="queryParams.name"
          placeholder="入驻商户名称"
          clearable
          @clear="handleQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户信用代码" prop="email" label-width="130px">
        <el-input
          v-model="queryParams.creditCode"
          placeholder="商户信用代码"
          clearable
          @clear="handleQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批状态" prop="auditStatus" label-width="100px">
        <el-select
          v-model="queryParams.auditStatus"
          size="small"
          placeholder="请选择"
          style="width:150px"
          @clear="handleQuery"
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="item in dict.type.audit_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tenant:tenantEnter:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tenant:tenantEnter:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tenant:tenantEnter:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="listTenant" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="租户名称" align="center" prop="name" />
      <el-table-column label="统一信用代码" align="center" prop="creditCode" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="入驻状态" align="center" prop="auditStatus" :formatter="enterFormat" />
      <el-table-column label="申请时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tenant:tenantEnter:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tenant:tenantEnter:remove']"
          >删除</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-collection-tag"
              @click="detailInfo(scope.row)"
              v-hasPermi="['tenant:tenantEnter:query']"
          ><el-badge is-dot class="item" :hidden="!(scope.row.auditStatus === 0)">详情</el-badge></el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="detailOpen" width="600px" append-to-body>
      <div slot="footer" class="dialog-footer" style="margin-top: -50px">
        <el-descriptions class="margin-top" :column="1" :size="size" border>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-user"></i>
              商户名称
            </template>
            {{this.form.name}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-coin"></i>
              社会统一信用代码
            </template>
            {{this.form.creditCode}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-office-building"></i>
              联系地址
            </template>
            {{this.form.address}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-coordinate"></i>
              营业执照
            </template>
            <div style="height: 170px;width: 350px;background: #7a6df0;margin: auto">
              <img v-bind:src="this.form.businessLicenseFileUrl">
            </div>
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-picture-outline"></i>
              名片
            </template>
            <div style="height: 170px;width: 350px;background: #7a6df0;margin: auto">
              <img v-bind:src="this.form.identifyFrontUrl">
            </div>
          </el-descriptions-item>
        </el-descriptions>
        <div slot="footer" class="dialog-footer" style="margin-top: 20px">
          <el-button type="primary" @click="approveTenantStatus(2)"  v-if="this.form.auditStatus == 0">同意</el-button>
          <el-button type="warning" @click="approveTenantStatus(1)"  v-if="this.form.auditStatus == 0">拒绝</el-button>
        </div>
      </div>
    </el-dialog>
    <!-- 添加或修改租户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="租户名称" prop="userName">
          <el-input v-model="form.name" placeholder="请输入用户名"/>
        </el-form-item>
        <el-form-item label="统一信用代码" prop="creditCode">
          <el-input v-model="form.creditCode" placeholder="请输入统一信用代码" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="form.address">
          </el-input>
        </el-form-item>
        <el-form-item label="营业执照" prop="businessLicenseFileUrl">
          <el-upload
            class="upload-template"
            drag
            :action="uploadImgUrl"
            :on-success="this.handleUploadSuccess"
            :limit="1"
            :file-list="fileList"
            :before-upload="beforeUploadFile"
            :on-error="handleUploadError"
            :on-exceed="handleExceed"
            ref="fileUpload"
            :show-file-list="true"
            :headers="headers">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip" style="margin-top: -20px">上传图片只能JPG、JPEG格式  !</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="名片" prop="businessCardUrl">
          <el-upload
            class="upload-template"
            drag
            :action="uploadImgUrl"
            :on-success="this.handleUploadSuccess"
            :limit="1"
            :file-list="fileList"
            :before-upload="beforeUploadFile"
            :on-error="handleUploadError"
            :on-exceed="handleExceed"
            ref="fileUpload"
            :show-file-list="true"
            :headers="headers">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip" style="margin-top: -20px">上传图片只能JPG、JPEG格式  !</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-top: -50px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTenant, selectTenant, approveTenant, deleteTenant} from "@/api/tenant/enter/tenant";
import {getUserProfile} from "@/api/auth/user";

export default {
  name: "enterTenant",
  dicts: ['sys_user_sex', 'audit_status'],
  data() {
    return {
      operationType: true,
      // 遮罩层
      loading: true,
      // 选中数组
      uids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //获取的目录列表
      listTenant:[],
      // 弹出层标题
      title: "",
      detailOpen: false,
      // 是否显示添加修改弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询模板列表 */
    getList() {
      this.loading = true;
      listTenant(this.queryParams).then(response => {
        this.listTenant = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //table表格格式化
    enterFormat(row, index){
      if (row.auditStatus === 0) {
        return "待审批";
      } else if(row.auditStatus === 1){
        return "审核未通过";
      }else{
        return "审核通过"
      }
    },

    detailInfo(row, index){
      this.title = "入驻详情"
      this.detailOpen = true;
      selectTenant(row.uid).then(response => {
        this.form = response.data;
      })

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.uids = selection.map(item => item.uid)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    approveTenantStatus: function(type){
      this.detailOpen = false
      let data = {};
      data.uid= this.form.uid;
      data.auditStatus = type;
      approveTenant(data).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加租户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "修改入驻信息";
      selectTenant(row.uid).then(response => {
        this.form = response.data;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.uid == undefined) {
            addTenantUser(this.form).then(response => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            });
          }else {
            updateTenantUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      let data = {};
      data.uidList = [];
      if(row.uid != undefined){
        data.uidList.push(row.uid)
      }else{
        data.uidList = this.uids;
      }
      this.$modal.confirm('是否确认删除租户编号为"' + data.uidList + '"的数据项？').then(function() {
        return deleteTenant(data);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
<style scoped>
/deep/ .el-textarea__inner {
  resize: none;
}
</style>
